var UserData = require('UserData')
cc.Class({
    extends: cc.Component,

    properties: {

    },
    
    popOut(){
        this.node.scale = 0
        this.node.parent.active = true
        this.node.active =true     
        this.node.runAction(cc.scaleTo(0.3,1,1).easing(cc.easeBackOut()))
        this.node.getChildByName("红龙").active = true
        this.node.getChildByName("红龙").getComponent('Float').float(cc.v2(283,326))
        this.updateGain()
        this.node.parent.getChildByName('金币奖励动画').getChildByName('棒呆了').getComponent(cc.Button).interactable = true
    },
    close(target,delayTime){
        if(delayTime && typeof(delayTime)=="string"){
            setTimeout(()=>{
                this.node.active =false
                this.node.parent.active =false
                this.successPrompt.active = false
            },parseInt(delayTime)*1000)
        }else{
            this.node.active =false
            this.node.parent.active =false
            this.successPrompt.active = false
        }
        
    },
    videoAdsNow(){
        try{
            let videoAd = wx.createRewardedVideoAd({
                adUnitId: 'adunit-c4a7bd71d9d72691'
            })
            
            videoAd.load()
            .then(() => videoAd.show())
            .catch(err => console.log(err.errMsg))

            videoAd.onClose((isEnded)=>{
                console.log(isEnded)
                if(isEnded){
                    this.node.active= false
                    this.shareEffect()
                }else{

                }
            })
            videoAd.onError((res)=>{
                console.log(res)
                if(res.errCode==1004){//无适合广告
                    //this.shareEffect()
                }else{
                    
                }
            })
        }catch(err){
            console.log(err)
        }
    },
    buttonAction(){
        if(this.videoAdsMode){
            this.videoAdsNow()
        }else{
            this.shareNow()
        }
    },
    shareNow(event,shareEffectFunction){
        
        try {
            let setting = {
                title: '你禁得住诱惑吗？',
                imageUrl: 'http://img.wenzhangba.com/allimg/180806/1401-1PP60524130.png',
                query: 'whoami=bbbbt'
            }
            if (UserData.runtimeState && UserData.runtimeState.shareSettings) {
                let randomIndex = Math.floor(Math.random() * UserData.runtimeState.shareSettings.length)
                setting = UserData.runtimeState.shareSettings[randomIndex]
            }
            wx.shareAppMessage(setting)
        } catch (err) {
            console.log(err)
        }
        
        //分享de效果
        //console.log("hide时间",UserData.runtimeState.tempLeaveTime)
        if(UserData.runtimeState.tempLeaveTime>2){ //离开大于2秒则75%几率分享成功
            if(Math.random()<0.75){
                if(shareEffectFunction){
                    shareEffectFunction()
                }else{
                    this.node.active= false
                    this.shareEffect()
                    console.log("shareGain")
                }
            }
        }else{
            if(Math.random()<0.3){  //离开小于2秒则30%几率分享成功
                if(shareEffectFunction){
                    shareEffectFunction()
                }else{
                    this.node.active= false
                    setTimeout(()=>{
                        this.shareEffect()
                    },3000)
                }
            }
        }
    },
    shareEffect(){
        this.successPrompt.active = true
    },
    addMoney(){
        
        UserData.addCurrentMoney(this.gain)
    },
    updateGain(){
        if(Math.random()<UserData.magicNumber){
            this.videoAdsMode = true
        }else{
            this.videoAdsMode = false
        }
        this.getComponent()
        this.gain = UserData.getCurrentMoney()*5+UserData.runtimeState.totalGPS *300
        this.gain = this.gain?this.gain:666
        this.successPrompt.getChildByName('金币label').getComponent('labelFixed')
        .setNumber(this.gain,'+','金币')
        if(this.videoAdsMode){
            this.node.getChildByName('引导语').getComponent('labelFixed').setNumber(this.gain,"观看一个完整的视频来获得","金币！")
            this.node.getChildByName('开始交易按钮').getChildByName('内容').getComponent(cc.Label).string = "观看"
            this.node.getChildByName('拒绝').scale = 1.8
        }else{
            this.node.getChildByName('引导语').getComponent('labelFixed').setNumber(this.gain,"告诉你的好友来赚取赠礼","金币！")
            this.node.getChildByName('开始交易按钮').getChildByName('内容').getComponent(cc.Label).string = "邀请"
            this.node.getChildByName('拒绝').scale = 1
        }
        this.successPrompt.getChildByName('金币奖励动画背景').runAction(cc.repeatForever(cc.rotateBy(3,-360)))
    },
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    getComponent(){
        this.successPrompt = this.node.parent.getChildByName('金币奖励动画')
    },
    start () {
        UserData.runtimeState.shareWindow = this
        this.node.scale = 0
        this.getComponent()
        this.successPrompt.active =false
        this.updateGain()


    },

    // update (dt) {},
});
